﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;

public partial class Admin_CountryMaster : System.Web.UI.Page
{
    #region events
    Guid gudUserID = CommonUtility.GetLoggedOnUserId();
    
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            GetData();
            //Session["UserID"] = Guid.NewGuid();
        }
    }

    private void GetData()
    {
        if (hdnInput.Value == "search")
        {
            DataSet objDs = new DataSet();
            objDs = GetDataSet();
            DataView objDv = new DataView();
            objDv = objDs.Tables[0].DefaultView;
            objDv.RowFilter = "CountryName  LIKE '*" + txtActivityTypeName.Text.Trim() + "*'";
            gvLanguage.DataSource = objDv;
        }
        else
        {
            gvLanguage.DataSource = GetDataSet();
        }
        gvLanguage.DataBind();
    }
    
    protected void gvSearchResult_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //throw new NotImplementedException();
        gvLanguage.PageIndex = e.NewPageIndex;
        GetData();
    }
 
    #endregion


    #region Methods
    private DataSet GetDataSet()
    {

        DataSet objDs=null;
        try
        {
            objDs = new CountryMasterBAL().GetCountryMaster();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;
       
    }

    


    



    protected void BtnClear_OnClick(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        txtActivityTypeName.Text = string.Empty;
        lblErrorMessage.Text = string.Empty;
        hdnInput.Value = string.Empty;
        //txtLanguageId.Text = "";
        GetData();
    }

    protected void btnAddNew_OnClick(object sender, EventArgs e)
    {
        //throw new NotImplementedException();

        Response.Redirect("~/Admin/CountryModify.aspx?lgid=0&mod=in");
    }

    protected void BtnSearch_OnClick(object sender, EventArgs e)
    {
        hdnInput.Value = "search";
        GetData();
    }

  
   
#endregion

    protected void gvLanguage_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvLanguage.PageIndex = e.NewPageIndex;
        GetData();
       
        
    }

    protected void gvLanguage_RowDataBound(object sender, GridViewRowEventArgs e)
    {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              LinkButton l = (LinkButton)e.Row.FindControl("lnkDelete");
              if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "IsActive")) == 0)
              {
                  l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to active " + DataBinder.Eval(e.Row.DataItem, "CountryName") + " Country Name?')");
              }
              else
              {
                  l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete " + DataBinder.Eval(e.Row.DataItem, "CountryName") + " Country Name?')");
              }
          }
    }

    //DELET lanugage method
    private void DeleteLanguage(int intLanguage, string strSetMethod)
    {

        CountryMasterBAL objLMBal;
        CountryMasterBO objLMBO;
        //Guid gudUserID = new Guid(Convert.ToString(Session["UserID"]));
        string strmod = Convert.ToString(Request.QueryString["mod"]);
        string strReturnValue;


        objLMBal = new CountryMasterBAL();
        objLMBO = new CountryMasterBO();

        objLMBO.SetMethod = strSetMethod;
        objLMBO.CountryID = intLanguage;
        //objLMBO.CountryName = txtActivityTypeName.Text.Trim();
        //objLMBO.CountryName=
        //objLMBO.ActivityCode = txtActivityTypeCode.Text.Trim();
      
        //objLMBO.CreatedBy = gudUserID;
        objLMBO.ModifyBy = gudUserID;
        objLMBO.ModifyDate = System.DateTime.Today;

        objLMBO.IsActive = (strSetMethod == "ACTIVE" ? true : false);
        strReturnValue = (string)objLMBal.SetCountryMaster(objLMBO.SetMethod, objLMBO.CountryID, objLMBO.CountryName, objLMBO.RegionID, objLMBO.CountryTelCode, objLMBO.CountryCode, objLMBO.CreatedBy, objLMBO.@ModifyBy, objLMBO.ModifyDate, objLMBO.IsActive);
        if (strReturnValue == "SUCCESS")
        {
            lblErrorMessage.Text = "Record has been updated succesfully.";
            lblErrorMessage.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            lblErrorMessage.Text = "Error while saving record.";
            lblErrorMessage.ForeColor = System.Drawing.Color.Red;
        }
    }


    //DELET grid event
    protected void gvLanguage_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int intLanguageID = (int)gvLanguage.DataKeys[e.RowIndex].Value;
        string strStatus = Convert.ToString(gvLanguage.Rows[e.RowIndex].Cells[2].Text).ToUpper() == "ACTIVE" ? "DELETE" : "ACTIVE";
        DeleteLanguage(intLanguageID, strStatus);
        GetData();
    }

    protected void gvLanguage_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvLanguage.PageIndex = e.NewEditIndex;
        GetData();
    }
}